package com.jingwu.example.service


import com.jingwu.example.pojo.dto.AwardDTO
import com.jingwu.example.pojo.entity.ActivityDO
import com.jingwu.example.pojo.entity.ActivityStageDO
import com.jingwu.example.repository.ActivityRepository
import org.spockframework.spring.SpringBean
import org.springframework.context.annotation.Import
import org.springframework.data.redis.core.MyDefaultHashOperations
import org.springframework.data.redis.core.RedisTemplate
import org.springframework.test.context.ContextConfiguration
import spock.lang.Specification

import javax.annotation.Resource

/**
 * @author 菁芜
 * @since 2021/11/20 - 16:08
 */
@Import([ActivityService.class])
@ContextConfiguration
class ActivityServiceTest extends Specification {
    @SpringBean
    ActivityRepository repository = Mock()
    @SpringBean
    RedisTemplate<String, Object> redisTemplate = Stub() {
        opsForHash() >> Mock(MyDefaultHashOperations)
    }
    @Resource
    ActivityService service

    def "award test"() {
        given:
        def dto = new AwardDTO()

        and:
        redisTemplate.opsForHash().get(_, _) >> null
        repository.selectById(_) >> Mock(ActivityDO)
        repository.exist(_, _, _) >> false
        repository.selectStage(_, _) >> Mock(ActivityStageDO)
        redisTemplate.opsForHash().put(_, _, _) >> null
        redisTemplate.expire(_, _, _) >> true

        when:
        service.award(dto)

        then:
        1 * repository.saveAward(_)
    }

}


/*
https://spockframework.org/spock/docs/1.3-RC1/all_in_one.html#_argument_constraints
Argument Constraints:
1 * subscriber.receive("hello")        // an argument that is equal to the String "hello"
1 * subscriber.receive(!"hello")       // an argument that is unequal to the String "hello"
1 * subscriber.receive()               // the empty argument list (would never match in our example)
1 * subscriber.receive(_)              // any single argument (including null)
1 * subscriber.receive(*_)             // any argument list (including the empty argument list)
1 * subscriber.receive(!null)          // any non-null argument
1 * subscriber.receive(_ as String)    // any non-null argument that is-a String
1 * subscriber.receive(endsWith("lo")) // any non-null argument that is-a String
1 * subscriber.receive({ it.size() > 3 && it.contains('a') })
// an argument that satisfies the given predicate, meaning that
// code argument constraints need to return true of false
// depending on whether they match or not
// (here: message length is greater than 3 and contains the character a)
*/
